package vip.xumy.admin.verify.conf;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.alibaba.fastjson.JSON;

import lombok.extern.log4j.Log4j2;
import vip.xumy.admin.verify.controller.PublicController;
import vip.xumy.core.pojo.com.AjaxResponse;

/**
 * 权限验证拦截器
 * @author v_mengyxu
 *
 */
@Log4j2
@Component
public class LoginInteceptor implements HandlerInterceptor {
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		//判断是否登录
		Object user = request.getSession().getAttribute(PublicController.KEY);
		if(user == null) {
			log.debug("Login interceptor : preHandle ...");
			response.setContentType("application/json;charset:UTF-8");
			response.getWriter().write(JSON.toJSONString(new AjaxResponse(false, "session timeout")));
			return false;
		}
		return true;
	}

}
